<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateNewDevelopmentRequest;
use App\Http\Requests\UpdateNewDevelopmentRequest;
use App\Models\NewDevelopmentType;
use App\Repositories\NewDevelopmentRepository;
use App\Http\Controllers\AppBaseController;
use App\Repositories\PicturesRepository;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class NewDevelopmentController extends AppBaseController
{
    /** @var  NewDevelopmentRepository */
    private $newDevelopmentRepository;
    private $pic;

    public function __construct(NewDevelopmentRepository $newDevelopmentRepo, PicturesRepository $picturesRepository)
    {
        $this->newDevelopmentRepository = $newDevelopmentRepo;
        $this->pic = $picturesRepository;
    }

    /**
     * Display a listing of the NewDevelopment.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->newDevelopmentRepository->pushCriteria(new RequestCriteria($request));
        $newDevelopments = $this->newDevelopmentRepository->paginate(20);

        return view('new_developments.index')
            ->with('newDevelopments', $newDevelopments);
    }

    /**
     * Show the form for creating a new NewDevelopment.
     *
     * @return Response
     */
    public function create()
    {
        $cats = NewDevelopmentType::all();
        return view('new_developments.create')->with('cats',$cats);
    }

    /**
     * Store a newly created NewDevelopment in storage.
     *
     * @param CreateNewDevelopmentRequest $request
     *
     * @return Response
     */
    public function store(CreateNewDevelopmentRequest $request)
    {
        $input = $request->all();
        $_image = $request->file('pic');
        if(!empty($_image)){
            $_res = $this->pic->uploadByHash($_image,"newdevelopments",null,null);
            $input['pic'] = $_res->url;
        }else{
            unset($input['pic']);
        };
        $newDevelopment = $this->newDevelopmentRepository->create($input);

        Flash::success('New Development saved successfully.');

        return redirect(route('newDevelopments.index'));
    }

    /**
     * Display the specified NewDevelopment.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $newDevelopment = $this->newDevelopmentRepository->findWithoutFail($id);

        if (empty($newDevelopment)) {
            Flash::error('New Development not found');

            return redirect(route('newDevelopments.index'));
        }

        return view('new_developments.show')->with('newDevelopment', $newDevelopment);
    }

    /**
     * Show the form for editing the specified NewDevelopment.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $newDevelopment = $this->newDevelopmentRepository->findWithoutFail($id);

        if (empty($newDevelopment)) {
            Flash::error('New Development not found');

            return redirect(route('newDevelopments.index'));
        }

        $cats = NewDevelopmentType::all();
        return view('new_developments.edit')->with('newDevelopment', $newDevelopment)->with('cats', $cats);
    }

    /**
     * Update the specified NewDevelopment in storage.
     *
     * @param  int              $id
     * @param UpdateNewDevelopmentRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateNewDevelopmentRequest $request)
    {
        $newDevelopment = $this->newDevelopmentRepository->findWithoutFail($id);

        if (empty($newDevelopment)) {
            Flash::error('New Development not found');

            return redirect(route('newDevelopments.index'));
        }
        $input = $request->all();
        $_image = $request->file('pic');
        if(!empty($_image)){
            $_res = $this->pic->uploadByHash($_image,"newdevelopments",null,null);
            $input['pic'] = $_res->url;
        }else{
            unset($input['pic']);
        };

        $newDevelopment = $this->newDevelopmentRepository->update($input, $id);

        Flash::success('New Development updated successfully.');

        return redirect(route('newDevelopments.index'));
    }

    /**
     * Remove the specified NewDevelopment from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $newDevelopment = $this->newDevelopmentRepository->findWithoutFail($id);

        if (empty($newDevelopment)) {
            Flash::error('New Development not found');

            return redirect(route('newDevelopments.index'));
        }

        $this->newDevelopmentRepository->delete($id);

        Flash::success('New Development deleted successfully.');

        return redirect(route('newDevelopments.index'));
    }
}
